{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6da6ff32",
   "metadata": {},
   "source": [
    "-------\n",
    "- 2022/09/13/week04\n",
    "- python 课堂笔记\n",
    "-------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5cedfc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 上周回顾(range()) :range：范围"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "504ac130",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 5)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b44659b",
   "metadata": {},
   "source": [
    "### for + range 决定循环的次数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e0bafc00",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range (5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fc8271c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(1, 5)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(1,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f6d861c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,5):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "77ec2673",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(1, 10, 3)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(1,10,3)  ## 3为步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5cba8ebb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "4\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10,3):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9772b070",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 本周内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfa6b44c",
   "metadata": {},
   "source": [
    "- type():检查类型\n",
    ">  1. 数值\n",
    ">> 1. int\n",
    ">> 2. float\n",
    ">> 3. complex\n",
    "\n",
    " >  2. 字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5b01744",
   "metadata": {},
   "source": [
    "# 1.number(数值)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6617fb2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "### int"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "33b59403",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9 <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "a = 9\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e4b6c3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "### float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "64f27d5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11.5 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "b = 11.5\n",
    "print(b,type(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9723451d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5+3j) <class 'complex'>\n"
     ]
    }
   ],
   "source": [
    "### complex(虚数，复数)\n",
    "c = 5 + 3j\n",
    "print(c,type(c))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2b512d4",
   "metadata": {},
   "source": [
    "# 2.字符串(string)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb9b2892",
   "metadata": {},
   "source": [
    ">1. 关于字符串的基本内容\n",
    ">2. 关于字符串的方法和函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c56d22b",
   "metadata": {},
   "source": [
    "# 2.1关于字符串的基本内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3cf3820d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jenny <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "name = \"Jenny\"\n",
    "print(name,type(name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6789be8e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Jenny <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "name = 'Jenny'\n",
    "print(name,type(name))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1a1c4ef",
   "metadata": {},
   "source": [
    "#### 双引号单引号，三引号的格式都可以"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "067c33cb",
   "metadata": {},
   "source": [
    "# 2.2关于字符串的方法及函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eab9b029",
   "metadata": {},
   "source": [
    "> 1. 字符串的定义\n",
    "> 2. in检查成员对象\n",
    "> 3. 字符串的拼接\n",
    "> 4. 字符串的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d7b7a9b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "爱是永不熄灭的业火\n"
     ]
    }
   ],
   "source": [
    "# 关于有序性\n",
    "title = \"爱是永不熄灭的业火\"\n",
    "print(title)\n",
    "# 有多少个字符串就有多少个索引值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ec3d0725",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'爱'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[0]  # 取单一值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "21c78ff5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'是'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[1] ## 取单一值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2adabd34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'火'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ac1f217a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(title) # 检查文本的总长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2a7d0493",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'爱是永不熄灭'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[0:6] # 取一定范围的值，这种方法我们称为切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4e42f59f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'爱是永不熄灭'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title[:6]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3a08b82",
   "metadata": {},
   "source": [
    "### enumerate 枚举，既可以获取到内容item，又可以获取到 索引值 index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c7bab878",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 爱\n",
      "1 是\n",
      "2 永\n",
      "3 不\n",
      "4 熄\n",
      "5 灭\n",
      "6 的\n",
      "7 业\n",
      "8 火\n"
     ]
    }
   ],
   "source": [
    "## 利用for循环，遍历文本信息\n",
    "for i,j in enumerate(title):\n",
    "    print(i,j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f1b9eba3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 爱\n",
      "1 是\n",
      "2 永\n",
      "3 不\n",
      "4 熄\n",
      "5 灭\n",
      "6 的\n",
      "7 业\n",
      "业火这个词在这段文本中\n",
      "8 火\n"
     ]
    }
   ],
   "source": [
    "# 思考，如何判断一段文本中是否存在“广州”，例如文本  title  中的内容,还可以确定其出现的次数\n",
    "for i,j in enumerate(title):\n",
    "    print(i,j)\n",
    "    if j == \"业\":\n",
    "        if title[i+1] == \"火\":\n",
    "            print(\"业火这个词在这段文本中\") "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa8e8256",
   "metadata": {},
   "source": [
    "### in 检查成员对象\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0605e9b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"业火\" in title "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33006b33",
   "metadata": {},
   "source": [
    "### 字符串的拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "16926f97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好,jenny\n"
     ]
    }
   ],
   "source": [
    "print(\"你好\" +\",\"+ \"jenny\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54274fd8",
   "metadata": {},
   "source": [
    "#### 字符串不可以和数字进行连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "99311776",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [20]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m你好\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mjenny\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "print(\"你好\" + 1 + \"jenny\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3c660533",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 <class 'int'>\n",
      "1 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(1,type(1))\n",
    "print(\"1\",type(\"1\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32f887e6",
   "metadata": {},
   "source": [
    "#### 字符串不可以用来运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "78676aea",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for /: 'str' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [23]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m3\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;241;43m/\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m2\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for /: 'str' and 'str'"
     ]
    }
   ],
   "source": [
    "\"3\"/\"2\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "800a1206",
   "metadata": {},
   "source": [
    "### 字符串的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "feb638bf",
   "metadata": {},
   "source": [
    "> 1. str.capitalize(): 可以将首字母转成大写\n",
    "\n",
    "> 2. str.count(): 数量？\n",
    "\n",
    "> 3. str.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "069c5bdf",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"marry\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "7ef66bcd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.capitalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2858f753",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name.count(\"r\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "63adcae8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'爱是永不熄灭的业火'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "c747faff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title.count(\"n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "900df7de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['爱是', '的业火']"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "title.split(\"永不熄灭\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "9da403d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cdff2a38",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "978aa2a2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43d2d7fd",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43422c7e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99862c1a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94ce13b5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "885feb26",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17f8bb99",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b346b47",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
